function flatData(data) {
  const temp = JSON.parse(JSON.stringify(data))
  if (data instanceof Array) {
    return temp.map(item => {
      for (const key in item) {
        if (typeof item[key] === 'object') {
          for (const key2 in item[key]) {
            item[key + key2.charAt(0).toUpperCase() + key2.slice(1)] = item[key][key2]
          }
          delete item[key]
        }
      }
      return item
    })
  } else {
    for (const key in temp) {
      if (typeof temp[key] === 'object' && !(temp[key] instanceof Array)) {
        for (const key2 in temp[key]) {
          temp[key + key2.charAt(0).toUpperCase() + key2.slice(1)] = temp[key][key2]
        }
        delete temp[key]
      }
    }
    return temp
  }
}

module.exports = {
  flatData
}
